$(function () {
    //上个页面带过来的id
    var orderBillId = $("#orderBillId").val();
    var reasonValue = $("#reasonValue").val();
    //发送请求查询数据进行回显
    $.post("/client/selectByOrderBillId?orderBillId=" +orderBillId , function (data) {
        if(data){
            $("#client_form").form("load",data);
        }
    });
    $.post("/clientCar/selectByOrderBillId?orderBillId=" + orderBillId,function (data) {
        //console.log(data);
        if(data){
         $("#car_form").form("load",data);
        }
    });
    $.post("/orderBill/selectByOrderBillId?orderBillId=" + orderBillId,function (data) {
        if(data.aplicant){
            data["aplicant.realname"] = data.aplicant.realname;
        }
        if(data){
            $("#time_form").form("load",data);
            if(reasonValue == 1){
                data.reviseReason = "车辆信息批改";
            }else{
                data.reviseReason = "投保人信息批改";
            }
            $("#revise_form").form("load",data);
        }
    });
    if(!reasonValue){
        $.post("/reviseinfo/selectReviseinfo?orderBillId="+ orderBillId,function (data) {
            if(data){
                $("#revise_form").form("load",data);
            }
        });
    }

    $("#revise-panel").panel({
            title: "批改页面外框",
            fit:true
    });
    $("#reviseinfo_panel").panel({
        title: "批改信息",
        collapsible:true
    });
    $("#client_panel").panel({
        title: "投保人信息",
        collapsible:true
    });
    $("#time_panel").panel({
        title: "时间信息",
        collapsible:true
    });
    $("#car_panel").panel({
        title: "车辆信息",
        collapsible:true,
        //发送请求查询数据进行回显 (行不通)
        //content:'<iframe src="/client/selectByOrderBillId?orderBillId=" + ${orderBillId} style="width:100%;height:100%" frameborder="0"></iframe>
    });
    //二级联动
    $('#selectedBrand').combobox({
        onSelect: function (row) {
            if (row != null) {
                $('#selectedModel').combobox({
                    url: "/carModel/selectChildByParentId?parentId=" + row.id
                });
            }
        }
    });
    //判断修改原因, 设置非修改表单为只读  (没效果)
    //$('input').textbox({'readonly':true});
    //$('input,select',$('form[name="client_form"]')).prop('readonly',true);
    //console.log($('input[id*="time"]'));//数组
    $('input[id*="time"]').datebox('readonly',true);
    if( reasonValue == 1){
        $('form[name="client_form"]').prop('readonly',true);
    }else{
        $('form[name="car_form"]').prop('readonly',true);
    }

    //保存 和 提交审核
    $("a[data-cmd]").on("click",function(){
        var cmd = $(this).data("cmd");
        var  url = "/reviseinfo/save?orderBillId="+orderBillId;
        if(cmd == "audit"){
            url = "/reviseinfo/save?orderBillId="+ orderBillId + "&status=7";
        }
        $("#revise_form").form("submit",{
            url:url,
            onSubmit:function(param){
                var client = $("input[name='aplicant.id']").val();
                param["clientId"] = client;
                return true;
            },
            success: function(data) {
                data = $.parseJSON(data);
                if(data.success){
                    $("#car_form").form("submit",{
                        url:"/clientCar/reviseCar?orderBillId=" +orderBillId,
                        onSubmit:function(param){
                            var carModelId = $("input[name='carModel_id']").val();
                            param["carModel.id"]=carModelId;
                            return true;
                        },
                        success: function(data) {
                            data = $.parseJSON(data);
                            if(data.success){
                                $("#client_form").form("submit",{
                                    url:"/client/riviseClient",
                                    success: function(data) {
                                        data = $.parseJSON(data);
                                        if(data.success){
                                            $.messager.alert("温馨提示",data.msg,"info");
                                            window.history.back(-1);
                                        }
                                    }
                                });
                            }
                        }
                    });
                }
            }
        });
    });
});